using DimensionsHelper.LanguageService.Symbols;
using DimensionsHelper.LanguageService.Syntax;

namespace DimensionsHelper.LanguageService;

public class AnalysisForStatement : AnalysisStatement
{
    internal AnalysisForStatement(ForStatement syntaxNode,
        AnalysisIdentifier counter,
        AnalysisRange range,
        AnalysisBlock body,
        AnalysisNode? parent) :
        base(AnalysisBoundKinds.ForStatement, syntaxNode, parent)
    {
        Syntax = syntaxNode;

        Counter = counter;
        Counter.Parent = this;
        
        Range = range;
        Range.Parent = this;

        Body = body;
        Body.Parent = this;
    }


    public override ForStatement Syntax { get; }

    public AnalysisIdentifier Counter { get; }

    public AnalysisRange Range { get; }

    public AnalysisBlock Body { get; }


    public override int ChildrenCount => 4;

    public override IEnumerable<AnalysisNode> GetChildren()
    {
        return [Counter, Range, Body];
    }
    
    internal override void AddChild(AnalysisNode child)
    {
        throw new NotSupportedException("AnalysisStatement is read-only.");
    }
}

public static partial class AnalysisNodeFactory
{
    /// <summary>
    /// 创建一个For循环语句
    /// </summary>
    /// <param name="syntaxNode">原始语法节点</param>
    /// <param name="counter">基数变量</param>
    /// <param name="range">循环区间</param>
    /// <param name="body">循环语句体</param>
    /// <param name="parent">父节点，可以忽略</param>
    /// <param name="withSymbol">语法节点所在的With语句上下文类型符号</param>
    public static AnalysisForStatement ForStatement(ForStatement syntaxNode,
        AnalysisIdentifier counter,
        AnalysisRange range,
        AnalysisBlock body,
        AnalysisNode? parent,
        ITypeSymbol? withSymbol)
    {
        return new AnalysisForStatement(syntaxNode, counter, range, body, parent)
            .WithContainingWithSymbol(withSymbol);
    }
}